POJ-1860 Currency Exchange(SPFA判正环)

描述

传送门:Currency Exchange

Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and performs exchange operations only with these currencies. There can be several points specializing in the same pair of currencies. Each point has its own exchange rates, exchange rate of A to B is the quantity of B you get for 1A. Also each exchange point has some commission, the sum you have to pay for your exchange operation. Commission is always collected in source currency.
For example, if you want to exchange 100 US Dollars into Russian Rubles at the exchange point, where the exchange rate is 29.75, and the commission is 0.39 you will get (100 - 0.39) * 29.75 = 2963.3975RUR.
You surely know that there are N different currencies you can deal with in our city. Let us assign unique integer number from 1 to N to each currency. Then each exchange point can be described with 6 numbers: integer A and B - numbers of currencies it exchanges, and real RAB, CAB, RBA and CBA - exchange rates and commissions when exchanging A to B and B to A respectively.
Nick has some money in currency S and wonders if he can somehow, after some exchange operations, increase his capital. Of course, he wants to have his money in currency S in the end. Help him to answer this difficult question. Nick must always have non-negative sum of money while making his operations.

输入描述

The first line of the input contains four numbers: N - the number of currencies, M - the number of exchange points, S - the number of currency Nick has and V - the quantity of currency units he has. The following M lines contain 6 numbers each - the description of the corresponding exchange point - in specified above order. Numbers are separated by one or more spaces. 1<=S<=N<=100, 1<=M<=100, V is real number, 0<=V<=103.
For each point exchange rates and commissions are real, given with at most two digits after the decimal point, 10-2<=rate<=102, 0<=commission<=102.
Let us call some sequence of the exchange operations simple if no exchange point is used more than once in this sequence. You may assume that ratio of the numeric values of the sums at the end and at the beginning of any simple sequence of the exchange operations will be less than 104.

输出描述

If Nick can increase his wealth, output YES, in other case output NO to the output file.

示例

输入

1
2
3
3 2 1 20.0
1 2 1.00 1.00 1.00 1.00
2 3 1.10 1.00 1.10 1.00

输出

1
YES

题解

题目大意

某城市有M(1<=M<=100)个货币兑换站,可以兑换N(1<=N<=100)种货币,每个兑换站只能互换两种货币,且汇率与手续费各不相同。某人初始时手中有V元货币S,问他是否有可能通过货币兑换,在最后换回货币S时实现盈利,有可能则输出YES,否则输出NO。

思路

抽象成图模型,兑换途径即为路径.
问题转换为判断图中是否存在一个正环.

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <stack>
#include <cmath>
#include <deque>
#include <queue>
#include <list>
#include <set>
#include <map>
#define debug() printf("WTF!\n");
#define mem(a, b) memset(a, b, sizeof(a))
typedef long long ll;
const double EPS = 1e-9;
const double PI = acos(-1);
const int INF = 0x3f3f3f3f;
const int MAXN = 1e3 +5;
using namespace std;
int n, m, s;
double v;
double dis[MAXN];
int vis[MAXN];

struct node{
int to;
double rate, comm;
node(int _to, double _rate, double _comm) : to(_to), rate(_rate), comm(_comm) {}
};
vector<node> E[MAXN];

bool SPFA(){
mem(dis, 0); mem(vis, 0);
queue<int> que;
que.push(s);
dis[s] = v; vis[s] = 1;
while(!que.empty()){
int now = que.front();
que.pop();
vis[now] = 0;
for(int i = 0; i < E[now].size(); i++){
int u = E[now][i].to;
if(dis[u] < (double)(dis[now] - E[now][i].comm) * E[now][i].rate){
dis[u] = (dis[now] - E[now][i].comm) * E[now][i].rate;
if(!vis[u]){
vis[u] = 1;
que.push(u);
}
}
}
if(dis[s] > v) return true;
}
return false;
}

int main(){
int a, b;
double c, d, e, f;
while(~scanf("%d %d %d %lf", &n, &m, &s, &v)){
for(int i = 0; i <= MAXN; i++) E[i].clear();
for(int i = 0; i < m; i++){
scanf("%d %d %lf %lf %lf %lf", &a, &b, &c, &d, &e, &f);
E[a].push_back(node(b, c, d));
E[b].push_back(node(a, e, f));
}
if(SPFA()) printf("YES\n");
else printf("NO\n");
}
}